"""
@Author: li
@Email: lijianqiao2906@live.com
@FileName: system.py
@DateTime: 2025/07/06
@Docs: 系统管理相关的 Pydantic Schema
"""

from datetime import datetime

from pydantic import Field

from app.schemas.base import BaseRequest


# UserSettings Schemas
class UserSettingsBase(BaseRequest):
    """用户设置基础 Schema"""

    theme: str = Field(default="light", max_length=20, description="主题设置")
    language: str = Field(default="zh-CN", max_length=10, description="语言设置")
    sidebar_collapsed: bool = Field(default=False, description="侧边栏折叠")
    page_size: int = Field(default=20, description="每页显示数量")
    timezone: str = Field(default="Asia/Shanghai", max_length=50, description="时区设置")
    notification_email: bool = Field(default=True, description="邮件通知")
    notification_sms: bool = Field(default=False, description="短信通知")
    custom_settings: dict | None = Field(default=None, description="自定义配置")
    description: str | None = Field(default=None, description="描述")


class UserSettingsCreate(UserSettingsBase):
    """创建用户设置 Schema"""

    user_id: int = Field(description="用户ID")


class UserSettingsUpdate(BaseRequest):
    """更新用户设置 Schema"""

    theme: str | None = Field(default=None, max_length=20, description="主题设置")
    language: str | None = Field(default=None, max_length=10, description="语言设置")
    sidebar_collapsed: bool | None = Field(default=None, description="侧边栏折叠")
    page_size: int | None = Field(default=None, description="每页显示数量")
    timezone: str | None = Field(default=None, max_length=50, description="时区设置")
    notification_email: bool | None = Field(default=None, description="邮件通知")
    notification_sms: bool | None = Field(default=None, description="短信通知")
    custom_settings: dict | None = Field(default=None, description="自定义配置")
    description: str | None = Field(default=None, description="描述")


class UserSettingsRead(UserSettingsBase):
    """读取用户设置 Schema"""

    id: int = Field(description="设置ID")
    user_id: int = Field(description="用户ID")

    class Config:
        from_attributes = True


# SystemConfig Schemas
class SystemConfigBase(BaseRequest):
    """系统配置基础 Schema"""

    key: str = Field(max_length=100, description="配置键")
    value: str | None = Field(default=None, description="配置值")
    category: str = Field(max_length=50, description="配置分类")
    type: str = Field(max_length=20, description="数据类型")
    is_public: bool = Field(default=False, description="是否公开")
    sort_order: int = Field(default=0, description="排序")
    description: str | None = Field(default=None, description="描述")


class SystemConfigCreate(SystemConfigBase):
    """创建系统配置 Schema"""

    pass


class SystemConfigUpdate(BaseRequest):
    """更新系统配置 Schema"""

    value: str | None = Field(default=None, description="配置值")
    category: str | None = Field(default=None, max_length=50, description="配置分类")
    type: str | None = Field(default=None, max_length=20, description="数据类型")
    is_public: bool | None = Field(default=None, description="是否公开")
    sort_order: int | None = Field(default=None, description="排序")
    description: str | None = Field(default=None, description="描述")


class SystemConfigRead(SystemConfigBase):
    """读取系统配置 Schema"""

    id: int = Field(description="配置ID")
    is_readonly: bool = Field(description="是否只读")

    class Config:
        from_attributes = True


# OperationLog Schemas
class OperationLogCreate(BaseRequest):
    """创建操作日志 Schema"""

    user_id: int | None = Field(default=None, description="操作用户ID")
    module: str = Field(max_length=50, description="操作模块")
    action: str = Field(max_length=50, description="操作类型")
    resource_id: str | None = Field(default=None, max_length=100, description="资源ID")
    resource_type: str | None = Field(default=None, max_length=50, description="资源类型")
    method: str = Field(max_length=10, description="请求方法")
    path: str = Field(max_length=500, description="请求路径")
    ip_address: str = Field(max_length=45, description="IP地址")
    user_agent: str | None = Field(default=None, description="用户代理")
    request_data: dict | None = Field(default=None, description="请求数据")
    response_code: int = Field(description="响应码")
    response_time: int = Field(description="响应时间(ms)")
    description: str | None = Field(default=None, description="描述")


class OperationLogRead(OperationLogCreate):
    """读取操作日志 Schema"""

    id: int = Field(description="日志ID")
    created_at: datetime = Field(description="创建时间")


# LoginLog Schemas
class LoginLogCreate(BaseRequest):
    """创建登录日志 Schema"""

    user_id: int = Field(description="用户ID")
    login_type: str = Field(max_length=20, description="登录类型")
    ip_address: str = Field(max_length=45, description="IP地址")
    location: str | None = Field(default=None, max_length=200, description="登录地点")
    device: str | None = Field(default=None, max_length=200, description="登录设备")
    user_agent: str | None = Field(default=None, description="用户代理")
    is_success: bool = Field(description="是否成功")
    failure_reason: str | None = Field(default=None, max_length=200, description="失败原因")
    description: str | None = Field(default=None, description="描述")
    login_at: datetime = Field(default_factory=datetime.now, description="登录时间")


class LoginLogRead(LoginLogCreate):
    """读取登录日志 Schema"""

    id: int = Field(description="日志ID")
    logout_at: datetime | None = Field(description="登出时间")

    class Config:
        from_attributes = True
